package com.industry.examinaion.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.industry.examinaion.dal.dataobject.ExaminaionPaper;
import com.industry.examinaion.dal.dataobject.ExaminaionPaperAnswer;
import com.industry.examinaion.dal.dataobject.ExaminaionPaperQuestion;
import com.industry.examinaion.dal.dto.SubmitPaperDto;
import com.industry.examinaion.service.ExaminaionPaperService;
import com.industry.framework.common.Result;
import com.industry.framework.common.page.TableDataInfo;
import com.industry.framework.core.annountions.monitor.IdooMonitorService;
import com.industry.framework.core.exception.CoreInvalidateArgsException;
import com.industry.framework.core.generic.AbstractCoreGenericController;
import com.industry.framework.core.wrapper.QueryTableWrapper;
import com.industry.framework.core.wrapper.WrapperExtractor;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("examinaion-paper")
public class ExaminaionPaperController extends AbstractCoreGenericController<ExaminaionPaper,String> {


    @Resource
    ExaminaionPaperService examinaionPaperService;

    /**
     * 列表查询重写 按id查询数据
     * @param queryTableWrapper
     * @return
     */
    @Override
    public Result<TableDataInfo<ExaminaionPaper>> queryList(@RequestBody QueryTableWrapper<ExaminaionPaper> queryTableWrapper) {
        try{
            WrapperExtractor<ExaminaionPaper> wrapperExtractor = new WrapperExtractor<>(ExaminaionPaper.class);
            QueryWrapper<ExaminaionPaper> examinaionPaperQueryWrapper = wrapperExtractor.queryWrapperBuilder(queryTableWrapper);
            //默认查询分给当前用户的的 （考生） 或 创建人为当前用户的（老师）
            examinaionPaperQueryWrapper.lambda()
                    .eq(ExaminaionPaper::getBelongsUserId, StpUtil.getLoginId())
                    .or()
                    .eq(ExaminaionPaper::getCreateUser,StpUtil.getLoginId().toString());
            //构造Page
            IPage<ExaminaionPaper> iPage = getService().getMapper().selectPage(wrapperExtractor.excuteRowBounds(), examinaionPaperQueryWrapper);
            return Result.success(TableDataInfo.build(iPage));
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail("服务故障");
        }
    }


    /**
     * 交卷
     */
    @PostMapping("/submit")
    @IdooMonitorService("交卷")
    //根据试卷查询 题目内容
    public Result<List<ExaminaionPaperQuestion>> submit(@RequestBody SubmitPaperDto submitPaperDto) {
        examinaionPaperService.submit(submitPaperDto);
        return Result.success();
    }
}
